package com.itfitness.opengl

import android.util.Rational
import androidx.camera.core.CameraX
import androidx.camera.core.Preview
import androidx.camera.core.PreviewConfig
import androidx.lifecycle.LifecycleOwner

/**
 *
 * @ProjectName:    OpenGL
 * @Package:        com.itfitness.opengl
 * @ClassName:      CameraHelper
 * @Description:     java类作用描述
 * @Author:         itfitness
 * @CreateDate:     2023-04-01 15:52
 * @UpdateUser:     更新者：
 * @UpdateDate:     2023-04-01 15:52
 * @UpdateRemark:   更新说明：
 */
class CameraHelper(
    lifecycleOwner: LifecycleOwner?,
    onPreviewOutputUpdateListener: Preview.OnPreviewOutputUpdateListener
) {
    companion object {
        private val lensFacing = CameraX.LensFacing.BACK
        private fun getPreview(listener: Preview.OnPreviewOutputUpdateListener): Preview {
            val previewConfig = PreviewConfig.Builder()
                .setTargetAspectRatio(Rational.NaN)
                .setLensFacing(lensFacing)
                .build()
            val preview = Preview(previewConfig)
            preview.onPreviewOutputUpdateListener = listener
            return preview
        }
    }

    init {
        CameraX.bindToLifecycle(lifecycleOwner, getPreview(onPreviewOutputUpdateListener))
    }
}